package user

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"sola/db"
	"sola/router/middleware"
	"sola/utils"
)

func login(c *gin.Context) {
	middleware.ProcessRequest(c)
	var err error
	var userInfo db.User
	defer func() {
		if err != nil {
			utils.ResponseError(c, utils.ErrCodeParameter)
			return
		}

		//用户登陆成功之后，需要把user_id设置到session当中
		middleware.SetUserId(userInfo.UserId, c)
		//当调用responseSuccess的时候，gin框架已经把数据发送给浏览器了
		//所以在responseSuccess之后，SetCookie就不会生效。因此，account.ProcessResponse
		//必须在util.ResponseSuccess之前调用
		middleware.ProcessResponse(c)
		//utils.ResponseSuccess(c, nil)
		user_info := make(map[string]interface{})
		user_info["code"] = 20000
		user_info["token"] = "admin-token"
		utils.ResponseSuccess(c, user_info)
	}()
	err = c.BindJSON(&userInfo)
	if err != nil {
		utils.ResponseError(c, utils.ErrCodeParameter)
		return
	}
	if len(userInfo.Username) == 0 || len(userInfo.Password) == 0 {
		utils.ResponseError(c, utils.ErrCodeParameter)
		return
	}
	err = db.Login(&userInfo)
	if err == db.ErrUserNotExists {
		utils.ResponseError(c, utils.ErrCodeUserNotExist)
		return
	}
	if err == db.ErrUserPasswordWrong {
		utils.ResponseError(c, utils.ErrCodeUserPasswordWrong)
		return
	}
	if err != nil {
		utils.ResponseError(c, utils.ErrCodeServerBusy)
		return
	}
	//c.JSON(http.StatusOK, user_info)
	return
}

func regist(c *gin.Context) {

	var userInfo db.User
	err := c.BindJSON(&userInfo)
	if err != nil {
		utils.ResponseError(c, utils.ErrCodeParameter)
		return
	}

	if len(userInfo.Username) == 0 || len(userInfo.Password) == 0 ||
		len(userInfo.Nickname) == 0 {
		utils.ResponseError(c, utils.ErrCodeParameter)
		return
	}

	err = db.RegistUser(&userInfo)
	if err == db.ErrUserExists {
		utils.ResponseError(c, utils.ErrCodeUserExist)
		return
	}
	fmt.Println("db err:", err)
	if err != nil {
		utils.ResponseError(c, utils.ErrCodeServerBusy)
		return
	}

	utils.ResponseSuccess(c, nil)
}

func logout(c *gin.Context) {
	user_info := make(map[string]interface{})
	user_info["code"] = 20000
	user_info["token"] = "admin-token"
	utils.ResponseSuccess(c, user_info)
	//c.JSON(http.StatusOK, user_info)
	return
}

func getUserInfo(c *gin.Context) {
	middleware.ProcessRequest(c)
	user_id, err := middleware.GetUserId(c)
	if err != nil {
		utils.ResponseError(c, utils.ErrCodeNotLogin)
		return
	}
	user_info := make(map[string]interface{})
	roles := make([]string, 0)
	roles = append(roles, "admin")
	user_info["roles"] = roles
	user_info["introduction"] = "I am a super administrator"
	user_info["avatar"] = "http://localhost:8000/images/0"
	user_info["name"] = "Super Admin"
	user_info["user_id"] = user_id
	//{
	//roles: ['admin'],
	//	introduction: 'I am a super administrator',
	//avatar: 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',
	//name: 'Super Admin'
	//}
	utils.ResponseSuccess(c, user_info)
	//c.JSON(http.StatusOK, user_info)
	return
}

func searchUser(c *gin.Context) {
	data := make(map[string]interface{})
	items := make([]string, 0)
	items = append(items, "Xuyi")
	items = append(items, "Connor")
	data["items"] = items
	//{
	//roles: ['admin'],
	//	introduction: 'I am a super administrator',
	//avatar: 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',
	//name: 'Super Admin'
	//}
	utils.ResponseSuccess(c, data)
	//c.JSON(http.StatusOK, user_info)
	return
}
